package com.example.demo.modules.customDefine.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.R;
import com.example.demo.modules.customDefine.entity.ModuleFieldData;
import com.example.demo.modules.customDefine.entity.ModuleFieldDataJson;
import com.example.demo.modules.customDefine.service.ModuleFieldDataJsonService;
import com.example.demo.modules.customDefine.service.ModuleFieldDataService;
import com.example.demo.modules.customDefine.vo.ModuleFieldDataJsonVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

import static com.example.demo.common.util.Utils.*;

@RestController
@RequestMapping("/api/moduleFieldData")
public class ModuleFieldDataController {

    @Autowired
    private ModuleFieldDataService moduleFieldDataService;
    @Autowired
    private ModuleFieldDataJsonService moduleFieldDataJsonService;


    @GetMapping("/listAll")
    public R listAll(@RequestParam(value = "supplierId", required = true) String supplierId) {
        QueryWrapper<ModuleFieldDataJson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", supplierId);
        ModuleFieldDataJson one = moduleFieldDataJsonService.getOne(queryWrapper);
        if (null != one) {
            return R.ok().put("result", one);
        } else {
            return R.error().put("msg", "目前没有数据");
        }

    }


    @GetMapping("/one")
    public R one(@RequestParam(value = "supplierId", required = true) String supplierId
    ) {
        QueryWrapper<ModuleFieldData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id", supplierId);
        return R.ok().put("result", moduleFieldDataService.getMap(queryWrapper));
    }


    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", dataType = "String")
    })
    public R page(
            Integer pageSize, Integer pageNum) {
        if (isNil(pageSize)) {
            pageSize = 10;
        }
        if (isNil(pageNum)) {
            pageNum = 0;
        }
        QueryWrapper<ModuleFieldData> queryWrapper = new QueryWrapper<>();
        return R.ok().put("result", moduleFieldDataService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @PostMapping("/add")
    public R add(@RequestBody ModuleFieldData moduleFieldData) {
        return R.ok().put("result", moduleFieldDataService.save(moduleFieldData));
    }

    @PostMapping("/update")
    public R update(@RequestBody ModuleFieldData moduleFieldData, HashMap<String, Object> updateOptions) {
        QueryWrapper<ModuleFieldData> queryWrapper = new QueryWrapper<>();
        if (!isNil(moduleFieldData.getId())) {
            queryWrapper.eq("id", moduleFieldData.getId());
        } else {
            return R.error("id不能为空");
        }
        return R.ok().put("result", moduleFieldDataService.update(moduleFieldData, queryWrapper));
    }

    @PostMapping("/updateBatch")
    public R updateBatch(@RequestBody List<ModuleFieldData> customDataValueList) {
        if (null != customDataValueList && customDataValueList.size() != 0) {
            R.ok().put("msg", "没有上传 customDataValueList");
        }
        return R.ok().put("result", moduleFieldDataService.saveOrUpdateBatch(customDataValueList));
    }

    @PostMapping("/updateBatchWithJson")
    public R updateBatchWithJson(
            @RequestBody ModuleFieldDataJsonVO moduleFieldDataJsonVO){

        if (null != moduleFieldDataJsonVO.getModuleFieldDataList() && moduleFieldDataJsonVO.getModuleFieldDataList().size() != 0) {
            R.ok().put("msg", "没有上传 customDataValueList");
        }

        ModuleFieldDataJson moduleFieldDataJson = new ModuleFieldDataJson();

        QueryWrapper<ModuleFieldDataJson> queryWrapper=new QueryWrapper<>();

        //查询到1
        queryWrapper.eq("user_id", moduleFieldDataJsonVO.getUserId());
        ModuleFieldDataJson one = moduleFieldDataJsonService.getOne(queryWrapper);
        if(null != one){
            //修改
            moduleFieldDataJson.setId(one.getId());
        }



        moduleFieldDataJson.setUserId(toInt(moduleFieldDataJsonVO.getUserId()));
        moduleFieldDataJson.setUserType(moduleFieldDataJsonVO.getUserType());
        //直接存在一个大的json
        moduleFieldDataJson.setJsonValue(tojsonStr(moduleFieldDataJsonVO.getModuleFieldDataList()));
        return R.ok().put("result", moduleFieldDataJsonService.saveOrUpdate(moduleFieldDataJson));
    }
}
